﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SportStore.Domain.Abstract;
using SportStore.Domain.Entities;
using SportStore.WebUI.Controllers;
using SportStore.WebUI.Models;

namespace SportStore.UnitTests
{
    [TestFixture]
    public class CatalogBrowsing
    {
        [Test]
        public void Can_View_A_Single_Page_Of_Products()
        {
            IProductsRepository repository = UnitTestHelpers.MockProductsRepository(
                new Product { Name = "P1" },
                new Product { Name = "P2" },
                new Product { Name = "P3" },
                new Product { Name = "P4" },
                new Product { Name = "P5" }
                );
            var controller = new ProductsController(repository);
            //Assumsion
            controller.PageSize = 3;

            //Act
            var result = controller.List(null, 2);

            //Assert
            var viewModel = (ProductsListViewModel)result.ViewData.Model;
            var displayedProducts = viewModel.Products;
            displayedProducts.Count.ShouldEqual(2);
            displayedProducts[0].Name.ShouldEqual("P4");
            displayedProducts[1].Name.ShouldEqual("P5");

        }

        [Test]
        public void Can_View_Products_From_All_Categories()
        {
            IProductsRepository repository = UnitTestHelpers.MockProductsRepository(
                new Product { Name = "p1", Category = "C1" },
                new Product { Name = "p2", Category = "C2" }
                );
            var controller = new ProductsController(repository);

            var result = controller.List(null, 1);

            var viewModel = (ProductsListViewModel)result.ViewData.Model;
            viewModel.Products.Count().ShouldEqual(2);
            viewModel.Products[0].Name.ShouldEqual("p1");
            viewModel.Products[1].Name.ShouldEqual("p2");
        }

        [Test]
        public void Can_View_Products_From_A_Single_Category()
        {
            IProductsRepository repository = UnitTestHelpers.MockProductsRepository(
                new Product { Name = "p1", Category = "C1" },
                new Product { Name = "p2", Category = "C2" }
                );
            var controller = new ProductsController(repository);
            var result = controller.List("C1", 1);
            var viewModel = (ProductsListViewModel)result.ViewData.Model;
            viewModel.Products.Count().ShouldEqual(1);
            viewModel.Products[0].Name.ShouldEqual("p1");
            viewModel.CurrentCategory.ShouldEqual("C1");

        }
    }
}
